package com.example.myone.Util;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.NumberPicker;

import androidx.appcompat.app.AlertDialog;

import com.example.myone.R;

public class DateDialogUtil {
    public static void showYearMonthPicker(Context context, DateSelectedListener listener) {
        // 加载自定义布局
        LayoutInflater inflater = LayoutInflater.from(context);
        View dialogView = inflater.inflate(R.layout.year_month_picker, null);
        // 初始化NumberPicker
        final NumberPicker yearPicker = dialogView.findViewById(R.id.year_picker);
        final NumberPicker monthPicker = dialogView.findViewById(R.id.month_picker);
        // 设置年份选择器的范围（例如，从1900到2100年）
        yearPicker.setMinValue(1900);
        yearPicker.setMaxValue(2100);
        yearPicker.setValue(2024); // 将默认值设置为当前年份
        // 设置月份选择器的范围（从1到12）
        monthPicker.setMinValue(1);
        monthPicker.setMaxValue(12);
        monthPicker.setDisplayedValues(new String[]
                {"01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12"});
        monthPicker.setValue(6); // 将默认值设置为当前月份
        // 创建并显示对话框
        new AlertDialog.Builder(context)
                .setTitle("选择日期")
                .setView(dialogView)
                .setPositiveButton("确认", (dialog, which) -> {
                    int selectedYear = yearPicker.getValue();
                    int selectedMonth = monthPicker.getValue();
                    listener.onDateSelected(selectedYear, selectedMonth);
                })
                .setNegativeButton("取消", null)
                .show();
    }


    public interface DateSelectedListener {
        void onDateSelected(int year, int month);
    }
}
